<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../../public/css/monokai_sublime.min.css">
    <link rel="stylesheet" href="../../public/css/heightlength.css">
    <link rel="stylesheet" href="../../public/css/mxjs.css">
    <link rel="stylesheet" href="../../public/css/prism.css">
    <script src="../../public/js/jquery.js"></script>
    <script src="../../public/js/highlight.min.js"></script>
    <script src="../../public/js/prism.js"></script>
    <script src="../../public/js/clipboard.min.js"></script>

</head>
<body>
<button>点击</button>
<div class="container">
    <div class="jsbg">
        <div class="col-lg-12 mxlength">
            <div class="mx-1">
                <h2>判断对象是否为空</h2>
                <code>
                    //举例说明两者区别
                    Person = function(name){
                    this.name = name || ''
                    }
                    Person.prototype.sayHello = function(){
                    console.log('hello')
                    }
                    p = new Person('yangyang')
                    p.age = 18
                    Object.defineProperties(p, {
                    age:{
                    enumerable:false
                    }
                    })
                    console.log(Object.keys(p)) // ["name"]
                    console.log(Object.getOwnPropertyNames(p)) // ["name", "age"]

                </code>
            </div>
        </div>
        <div class="col-lg-12  ">
      <pre class="mScroll">
        <code class="lang-javascript">
    function empty(obj){
        if(Object.keys(obj).length>0){
            return false;
        }else{
            return true;
        }
    }
    let obj={}
    let obj2={name:1,age:2,name:4}
    console.log(empty(obj)) //false
    console.log(Object.keys(obj2)) //[name,age]
    console.log(Object.getOwnPropertyNames(obj2))//[name,age]


            1.将json对象转化为json字符串，再判断该字符串是否为"{}"

var data = {};

var b = (JSON.stringify(data) == "{}");

alert(b);//true



2.for in 循环判断

var obj = {};

var b = function() {
for(var key in obj) {
return false;
}
return true;

}

alert(b());//true



3.jquery的isEmptyObject方法

此方法是jquery将2方法(for in)进行封装，使用时需要依赖jquery

var data = {};

var b = $.isEmptyObject(data);

alert(b);//true



4.Object.getOwnPropertyNames()方法

此方法是使用Object对象的getOwnPropertyNames方法，获取到对象中的属性名，存到一个数组中，返回数组对象，我们可以通过判断数组的length来判断此对象是否为空

注意：此方法不兼容ie8，其余浏览器没有测试

var data = {};

var arr = Object.getOwnPropertyNames(data);

alert(arr.length == 0);//true



5.使用ES6的Object.keys()方法

与4方法类似，是ES6的新方法, 返回值也是对象中属性名组成的数组

var data = {};

var arr = Object.keys(data);

alert(arr.length == 0);//true
		</code>
    </pre>
        </div>
    </div>
</div>
<script>

    window.onload = function () {
        var clipboard = new Clipboard('.toolbar-item');
        clipboard.on('success', function (e) {
            console.log(e.text);
        });
        clipboard.on('error', function (e) {
            console.log(e);
        });
        $(".toolbar-item").html("<span>复制</span>")
    }
</script>
</body>
</html>
